<template>
  <div>

    <el-dialog title="编辑分类" :visible="showDialog" width="30%" @close="closeFn">
      <el-form ref="ruleForm" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" />
        </el-form-item>
        <el-form-item label="描述" prop="desc">
          <el-input v-model="form.desc" type="textarea" />
        </el-form-item>
        <el-form-item label="图片" prop="image">
          <Upload />
          <el-input v-model="form.image" type="textarea" />
        </el-form-item>

      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-row type="flex" justify="center">
          <el-button @click="closeFn">取 消</el-button>
          <el-button type="primary" @click="addClifty">确 定</el-button>
        </el-row>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { addClistify, OneClistify, editClistify } from '@/api/model/classity'
import Upload from '@/components/Upload'
export default {
  components: { Upload },
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }

  },
  data() {
    return {
      form: {
        title: '',
        desc: '',
        image: '',
        id: null
      },
      rules: {
        title: [
          { required: true, message: '请输入标题', trigger: 'blur' }
        ],
        desc: [{ required: true, message: '请输入m=描述', trigger: 'blur' }]
      }
    }
  },
  created() {

  },
  methods: {
    async getClassFity(id) {
      this.form.id = id
      const res = await OneClistify(id)
      // console.log(res)
      this.form = res.data
    },
    // 新增
    async addClifty() {
      console.log(this.form.id)
      // this.$emit('addClifty', this.form)
      if (this.form.id) {
        await editClistify(this.form)
        this.$emit('updated')
        // console.log(res)
      } else {
        await addClistify(this.form)
      }

      this.closeFn()
    },
    // 关闭弹窗
    closeFn() {
      this.$emit('update:showDialog', false)
      this.form = {
        name: '',
        describe: ''
      }
      this.$refs.ruleForm.resetFields()
    }
  }
}
</script>
<style>

</style>
